﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace Microsoft.WindowsAzure.Search
{
    public class Parser
    {
        public static List<String> Parse(String content)
        {
            String[] lines = Tokenizer.LineTokenize(content);
            List<String> tokens = new List<String>();

            String line;
            foreach (String eachLine in lines)
            {
                line = Filter.UppercaseFilter(eachLine);
                line = Filter.NonwordsFilter(line);

                if (!Filter.IsBlankLine(line))
                {
                    List<String> lineTokens = Tokenizer.WhitespaceTokenize(line).ToList();
                    for (int i = 0; i < lineTokens.Count; i++)
                        lineTokens[i] = Lemmatizer.Lemmatize(lineTokens[i]);
                    Filter.StopwordFilter(lineTokens);
                    tokens.AddRange(lineTokens);
                }
            }
            return tokens;
        }

        public static Dictionary<String, int> ParseQuery(String queryInput)
        {
            List<String> queryTerms = Parse(queryInput);
            Dictionary<String, int> result = new Dictionary<string, int>();

            foreach (String term in queryTerms)
            {
                if (!result.ContainsKey(term))
                    result.Add(term, 1);
                else
                    result[term]++;
            }

            return result;
        }
    }
}
